<?php
/**
 * Message.php
 *
 * PHP Version 5.6
 *
 * @category PHP
 * @package  Object
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 * @date     2018-05-13 22:56:47
 * @modifyby ctwj
 */

namespace Telegram\Object;

use Telegram\Exception\AttributeException;

/**
 * Message class
 *
 * 消息
 *
 * @category PHP
 * @package  Object
 * @author   ctwj <908504609@qq.com>
 * @license  MIT https://mit-license.org/
 * @link     MIT https://mit-license.org/
 */
class Message
{
    /**
     * 和message表里面字段对应
     *
     * @var [type]
     */
    private $_id;
    private $_resource_id;
    private $_hashkey;
    private $_body_id;
    private $_message_url;
    private $_title;
    private $_abstract;
    private $_pic;
    private $_thumbnail;
    private $_ispost = false;
    private $_author = '';
    private $_category = '';
    private $_vist_times = 0;
    private $_tele_likes = 0;
    private $_tele_unlikes = 0;
    private $_pub_date = '';
    private $_post_date = '';
    private $_ins_time;
    /**
     * 该字段保存正文，将保存在body表
     *
     * @var [string]
     */
    private $_content;
    /**
     * 额外信息 parse_mode
     */
    private $_extinfo;

    /**
     * 初始化值
     *
     * @param [type] $data message数据
     *
     * @return void
     */
    public function __construct($data)
    {
        foreach ($data as $k => $v) {
            $k = '_' . $k;
            $this->$k = $v;
        }
    }

    /**
     * 获取可编辑字段
     *
     * @return array
     */
    public function getKeys()
    {
        return [
            'resource_id','body_id','message_url', 'title', 'abstract',
            'pic', 'thumbnail', 'ispost', 'vist_times', 'tele_likes',
            'tele_unlikes', 'author', 'category', 'content', 'pub_date', 'post_date'
        ];
    }

    /**
     * 设置属性的值
     *
     * @param [string] $key   key
     * @param [string] $value value
     *
     * @return void
     */
    public function __set($key, $value)
    {
        if (in_array($key, $this->getKeys())) {
            $key = '_' . $key;
            $this->$key = $value;
        } else {
            \getService('logger')->debug('Try Set a unset attribute', [$key => $value]);
        }
    }

    /**
     * 获取属性
     *
     * @param [string] $key key
     *
     * @return mixed
     */
    public function __get($key)
    {
        $key = '_' . $key;
        if (property_exists($this, $key)) {
            return $this->$key;
        }
        getService('logger')->error('Attribute Error', ['Message' => $key]);
        throw new AttributeException('Resource', $key);
    }

    /**
     * 获取数组
     *
     * @return array
     */
    public function getArray()
    {
        return [
            'id'            => $this->_id,
            'resource_id'   => $this->_resource_id,
            'hashkey'       => $this->_hashkey,
            'body_id'       => $this->_body_id,
            'message_url'   => $this->_message_url,
            'title'         => $this->_title,
            'abstract'      => $this->_abstract,
            'pic'           => $this->_pic,
            'thumbnail'     => $this->_thumbnail,
            'ispost'        => $this->_ispost,
            'author'        => $this->_author,
            'category'      => $this->_category,
            'vist_times'    => $this->_vist_times,
            'tele_likes'    => $this->_tele_likes,
            'tele_unlikes'  => $this->_tele_unlikes,
            'ins_time'      => $this->_ins_time,
            'pub_date'      => $this->_pub_date,
            'post_date'     => $this->_post_date,
            'content'       => $this->_content
        ];
    }
}
